package cn.wolfcode.ssm.service.impl;

import cn.wolfcode.ssm.domain.Role;
import cn.wolfcode.ssm.mapper.RoleMapper;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Override
    public void delete(Long id) {
        roleMapper.deleteByPrimaryKey(id);
        roleMapper.deleteRelation(id);
    }

    @Override
    public void save(Role role,Long[] ids) {
        roleMapper.insert(role);
        if(ids!=null&&ids.length>0){
            for(Long pid:ids){
                roleMapper.insertRelation(role.getId(),pid);
            }
        }

    }

    @Override
    public Role getOne(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> getAll() {
        return roleMapper.selectAll();
    }

    @Override
    public void update(Role role,Long[] ids) {
        roleMapper.updateByPrimaryKey(role);
        roleMapper.deleteRelation(role.getId());
        //保证ids要是一个有数据的数组
        if(ids!=null&&ids.length>0){
            for(Long pid:ids){
                roleMapper.insertRelation(role.getId(),pid);
            }
        }
    }

    @Override
    public PageInfo<Role> selectForPageResult(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Role> roles = roleMapper.selectForResult(qo);
        return new PageInfo<>(roles);
    }
}
